package com.jorado.etl.service;

import com.jorado.core.Result;
import com.jorado.etl.model.EtlInput;
import com.jorado.etl.repository.SolrPositionRepository;
import com.jorado.event.EventClient;
import com.jorado.event.dubbo.util.SpringUtils;
import com.jorado.search.core.service.impl.ExportToFileImpl;
import org.springframework.context.annotation.Description;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

@Description("Solr数据库语料抽取服务")
public final class SolrDBEtlService extends ExportToFileImpl<Map<String, Object>> {

    private int total;
    private int waitTime;
    private EtlService etlService;

    public SolrDBEtlService(String distFile) {
        this(distFile, true, 0);
    }

    public SolrDBEtlService(String distFile, boolean deleteIfExists, int waitTime) {
        super(SpringUtils.getBean(SolrPositionRepository.class), distFile, deleteIfExists);
        this.waitTime = waitTime;
        this.etlService = new EtlService(this.distFile);
        this.setAfterFilter(this::after);
    }

    @Override
    protected void export(List<Map<String, Object>> dataList) {

        total += dataList.size();

        HashSet<EtlInput> etlList = new LinkedHashSet<>();

        String jobTitle = "";
        String jobType = "";
        String industry = "";

        for (Map<String, Object> row : dataList) {

            jobTitle = row.get("JOB_TITLE").toString();
            jobType = row.get("JOB_TYPE_NAME").toString();
            industry = row.get("INDUSTRY_NAME").toString();

            //String jobDescription = row.get("JOB_DESC").toString();

            EtlInput etlInput = new EtlInput();
            etlInput.setJobTitle(jobTitle);
            etlInput.setJobType(jobType);
            etlInput.setIndustry(industry);

            //etlInput.setJobDescription(jobDescription);

            etlList.add(etlInput);

        }

        this.etlService.save(etlList);

        try {
            Thread.sleep(waitTime);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected Result after(Result result) {

        EventClient.getDefault().asyncSubmitLog(String.format("共抽取solr数据库职位:[%d]个", total));

        //TODO:对文件去重
        return Result.OK;
    }

}
